package com.kfm.jdbc.day0403;

import java.io.*;

public class Demo08 {

    public static void main(String[] args) {
        // 通过序列化实现深拷贝
        Computer computer = new Computer();
        computer.name = "联想";
        computer.price = 5000.0;
        Cpu cpu = new Cpu();
        cpu.name = "i7-7700";
        computer.cpu = cpu;

        System.out.println(computer);

        try {
            // 序列化
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);
            oos.writeObject(computer);
            oos.close();

            byte[] bytes = byteArrayOutputStream.toByteArray();

            // 反序列化
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
            Computer clone = (Computer) ois.readObject();

            System.out.println(clone);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
